home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / eTNote.subproj / eTNote.m < prev    next >
Encoding:
Text File  |  1995-02-11  |  7.1 KB  |  245 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    eTNote.m 
  3. //    SUMMARY:    Implementation of a rtf note annotation
  4. //    SUPERCLASS:    eTImage
  5. //    INTERFACE:    None
  6. //    PROTOCOLS:    <Tool, Annotation, ASCIISupport,InspectableTarget>
  7. //    AUTHOR:        Rohit Khare
  8. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  9. ///////////////////////////////////////////////////////////////////////////////
  10. //    IMPLEMENTATION COMMENTS
  11. //        Notes hold an RTF representation of some comments. ASCII conversion is
  12. //  done as in eTDocInfo. Date is a last-modified string.
  13. ///////////////////////////////////////////////////////////////////////////////
  14. //    HISTORY
  15. //    10/30/94:    Modified to support <InspectableTarget>
  16. //  07/27/94:    Still trying to tack down imag-loading bug,usd loadFromFile...
  17. //  07/07/94:    Converted to imageAnnotation by RK and TRZ 
  18. //  07/03/94:   HTMD Support by TRZ.
  19. //  05/22/94:    Created. First actual implementation.
  20. ///////////////////////////////////////////////////////////////////////////////
  21.  
  22. #import "eTNote.h"
  23. #define _eTNoteVERSION    10
  24.  
  25. NXAtom fileHack;
  26.  
  27. @implementation eTNote
  28. + toolAwake:theApp
  29. {
  30.     char        buf[MAXPATHLEN];
  31.     NXBundle    *bundle;
  32.     id            icon=nil;
  33.     
  34.     bundle = [NXBundle bundleForClass:[eTNote class]];
  35.     if ([bundle getPath:buf forResource:"eTNoteIcon" ofType:"tiff"] ) {
  36.         icon = [[NXImage alloc] initFromFile:buf];
  37.         [icon setName:"eTNoteIcon"];
  38.         if ([icon lockFocus]) [icon unlockFocus];
  39.     } else {
  40.         NXLogError("Image not found: eTNoteIcon");
  41.     }
  42.     [theApp   registerAnnotation: [eTNote class] 
  43.                             name: "Note"
  44.                     RTFDirective: "eTNote"
  45.                        menuLabel: "Insert Note..."
  46.                          menuKey: '\0'
  47.                         menuIcon: (NXImage *) icon];
  48.     return self;
  49. }
  50.  
  51.  
  52. #define DEFAULTRTF "{\\rtf0\\ansi{\\fonttbl\\f1\\fnil Times-Roman;\\f2\\fswiss Helvetica;}\n\\margl40\n\\margr40\n\\pard\\tx520\\tx1060\\tx1600\\tx2120\\tx2660\\tx3200\\tx3720\\tx4260\\tx4800\\tx5320\\f1\\b0\\ulnone\\fs24\\fc0\\cf0 Rich-Text \n\\b comments\n\\b0  about this document\n}\n"
  53. - init 
  54. {
  55.     [super init];
  56.     [self setUsesButtonStyle:NO];
  57.     [self setDraggable:YES];
  58.     [self setImageComponent:[eTImageComponent newImageNamed:"etNoteIcon"]];
  59.  
  60.     RTFRep = (char *)malloc((strlen(DEFAULTRTF)+1)*sizeof(char));
  61.     strcpy(RTFRep,DEFAULTRTF);
  62.     return self;
  63. }
  64. - free
  65. {
  66.     free(RTFRep);
  67.     return self = [super free];
  68. }
  69. - initFromPboard:thePB inDoc:theDoc linked:(BOOL) linked 
  70. {
  71.     struct    passwd *p;
  72.  
  73.     [super initFromPboard:thePB inDoc:theDoc linked:linked];
  74.     //[self init];
  75.     [imageComponent setDoc:theDoc];
  76.     [etDoc registerNotification:self];    
  77.     p = getpwuid(getuid());
  78.     if (p->pw_name)
  79.         strncpy(writer,p->pw_gecos,128);
  80.     else strncpy(writer,"Author's Name",128);
  81.     strncpy(date,[NXApp date],64);
  82.     return self;
  83. }
  84. - drag: (Pasteboard *)draggingPboard image: (NXImage **)proxyImage
  85. {
  86.     char     filename[MAXPATHLEN];
  87.     NXStream *t;
  88.     
  89.     // write out .rtf file in /tmp
  90.     sprintf(filename, "/tmp/eTNote.%d.rtf", [NXApp uniqueID]);
  91.  
  92.     [[eTNoteUI new] sync];
  93.     t = NXOpenMemory(RTFRep, strlen(RTFRep), NX_READONLY);
  94.     [theText readRichText:t];
  95.     NXClose(t);
  96.     [theText writeRTFtoPath:filename];
  97.     
  98.     [draggingPboard declareTypes:&NXFilenamePboardType num:1 owner:nil];
  99.     [draggingPboard writeType: NXFilenamePboardType data: filename
  100.                     length: strlen(filename)];
  101.     //NXLogError("NXImage got %x", [NXImage findImageNamed:NXUniqueString("eTNoteIcon")]);
  102.     *proxyImage = [NXImage findImageNamed:NXUniqueString("eTNoteIcon")];
  103.     //NXLogError("proxyImage got %x", *proxyImage);
  104.     return self;
  105. }
  106. - inspect: (NXEvent*)e
  107. {    [[NXApp inspector] inspect:self];
  108.     return self;
  109. }
  110. - (id <Inspectable>) inspectableDelegate {
  111.     return [[eTNoteUI new] setNote:self]; }
  112.  
  113. - readRichText:(NXStream *)stream forView:view 
  114. {
  115.     int i;
  116.     
  117.     NXScanf(stream, "%d ", &i);
  118.     if (i != _eTNoteVERSION) {
  119.         // bad version block.
  120.      NXLogError("eTNote found unparseable version %d at position %d",
  121.                     i, NXTell(stream));
  122.         return nil;
  123.     }
  124.     
  125.     NXScanf(stream, "%d", &i); NXGetc(stream); //space-eater
  126.     if (i) NXRead(stream, writer, i);
  127.     writer[i] = 0;
  128.     
  129.     NXScanf(stream, "%d", &i); NXGetc(stream); //space-eater
  130.     if (i) NXRead(stream, date,i);
  131.     date[i] = 0;
  132.  
  133.     NXScanf(stream, "%d", &i); NXGetc(stream); //space-eater
  134.     if (i) {
  135.         RTFRep = realloc(RTFRep,(i+1)*sizeof(char));
  136.         NXRead(stream, RTFRep, i);
  137.     }
  138.     RTFRep[i]=0;
  139.     NXGetc(stream); //trailing space
  140.     [super    readRichText:stream forView:view];
  141.     return self;
  142. }
  143.  
  144. - writeRichText:(NXStream *)stream forView:view
  145. {
  146.     [[eTNoteUI new] sync];
  147.     NXPrintf(stream, "%d %d %s %d %s %d %s ",
  148.      _eTNoteVERSION,strlen(writer),writer, strlen(date), date, strlen(RTFRep), RTFRep);
  149.     return [super writeRichText:stream forView:view];
  150. }
  151.  
  152. - writeASCIIRef:(NXStream *)stream forView:view
  153. {
  154.     NXStream *t;
  155.     id aText = [NXApp sharedText];
  156.  
  157.     [[eTNoteUI new] sync];
  158.     t = NXOpenMemory(RTFRep, strlen(RTFRep), NX_READONLY);
  159.     [aText readRichText:t];
  160.     NXClose(t);
  161.     NXPrintf(stream, "%y made this comment, last modified on %s:\n", writer, date);
  162.     [aText writeText:stream];
  163.     NXPrintf(stream, "\n\t");
  164.     [super writeASCIIRef:stream forView:view];
  165.     return self;
  166. }
  167.  
  168. - writeHTML:(NXStream *)stream forView:view
  169. {
  170.     char temp[MAXPATHLEN];
  171.     char noteFile[MAXPATHLEN];    
  172.  
  173.     strcpy(temp, [[[view etDoc] docInfo] docPath]);
  174.     *rindex(temp,'.')=0;
  175.     sprintf(noteFile,"%s."HTMD_EXT"/eTNote.%x",temp,[NXApp uniqueID]); 
  176.     NXPrintf(stream, "<A HREF=\"%V\">", noteFile);
  177.     [super writeHTML:stream forView:view];
  178.     NXPrintf(stream, "</A>");
  179.     fileHack = NXUniqueString(noteFile);
  180.     return self;
  181. }
  182. - writeLaTeX:(NXStream*)stream forView:view
  183. {    
  184.     char temp[MAXPATHLEN];
  185.     char noteFile[MAXPATHLEN];    
  186.  
  187.     [super writeLaTeX:stream forView:view];
  188.     strcpy(temp, [[[view etDoc] docInfo] docPath]);
  189.     *rindex(temp,'.')=0;
  190.     sprintf(noteFile,"%s."HTMD_EXT"/eTNote.%x",temp,[NXApp uniqueID]); 
  191.     NXPrintf(stream, "\footnote{See the file %w for a comment by %w on %w", noteFile,writer,date);
  192.     fileHack = NXUniqueString(noteFile);
  193.     return self;
  194. }
  195.  
  196. - writeComponentToPath:(char *)thePath inFormat:(int)fmt 
  197. {
  198.     char         temp[MAXPATHLEN];
  199.     char        noteFile[MAXPATHLEN];
  200.     id             aText;
  201.     NXStream    *t;
  202.     NXRect        rect;
  203.  
  204.     if(!etDoc)    NXLogError("etDoc is nil at %s %u",__FILE__,__LINE__);
  205.  
  206.     [super writeComponentToPath:thePath inFormat:fmt];
  207.  
  208.     if(fmt==HTMD_FMT || fmt==TeXD_FMT) {
  209.         aText = [[eText alloc] init];
  210.         rect.origin.x = rect.origin.y = 0;
  211.         rect.size.height = rect.size.width = MAXFLOAT;
  212.         [aText initFrame:&rect];
  213.         t = NXOpenMemory(RTFRep, strlen(RTFRep), NX_READONLY);
  214.         [aText readRichText:t];
  215.         NXClose(t);
  216.         [aText setSel:0:0];
  217.         
  218.         sprintf(noteFile, "%s made this comment, last modified on %s:\n", writer, date);
  219.         [aText replaceSel:noteFile];
  220.         
  221.         t = NXOpenMemory(NULL, 0, NX_READWRITE);
  222.         if(fmt==HTMD_FMT)     [aText writeHTML:t withTags:NULL];
  223.         if(fmt==TeXD_FMT)    [aText writeLaTeX:t    withTags: NULL];
  224.         aText = [aText free];
  225.         sprintf(temp,"%s/%s",thePath,fileHack);
  226.         NXSaveToFile(t,temp);
  227.         NXCloseMemory(t,NX_FREEBUFFER);
  228.     }
  229.     return self;
  230. }
  231.  
  232. - setRTFRep:(const char *)newRep 
  233. {
  234.     RTFRep = realloc(RTFRep,strlen(newRep));
  235.     strcpy(RTFRep,newRep); 
  236.     strncpy(date,[NXApp date],64); 
  237.     return self;
  238. }
  239.  
  240.  
  241. - (const char *)RTFRep {return RTFRep;}
  242. - (const char *)owner{return writer;}
  243. - setOwner:(const char *)newOwner{strncpy(writer,newOwner,128); return self;}
  244. - (const char *)date{return date;}
  245. @end